﻿using System;
using System.Linq;
using System.Web.Security;
using System.Web.UI.WebControls;
using NetDon.OA.Business;
using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Interface;
using NetDon.OA.Interface.Configuration;

using NetDon.Utilities;


namespace NetDon.OA.Portal
{
    public partial class Default : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var context = ServiceContainer.GetService<ApplicationContext>();
                lblUserName.Text = new UserEntity(context.UserId).FullName;
                LoadMenu();
            }
        }

        private void LoadMenu()
        {
            var context = ServiceContainer.GetService<ApplicationContext>();
            var service = ServiceContainer.GetService<ISecurityService>();
            var collection = service.GetPermitModules(context.UserId);
            //var reportModule = GetReportModule();
            //if (reportModule != null)
            //{
            //    collection.Add(reportModule);
            //}
            //var dataImportModule = GetDataImportModule();
            //if (dataImportModule != null)
            //{
            //    collection.Add(dataImportModule);
            //}
            rptMenu.DataSource = from entity in collection where entity.Visible == true select entity;
            rptMenu.DataBind();

            //Load first visible menu
           

        }

        protected void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) &&
                e.Item.ItemType != ListItemType.SelectedItem) return;
            var rptChildren = e.Item.FindControl("rptChildren") as Repeater;
            if (rptChildren == null) return;
            var module = e.Item.DataItem as Module;
            if (module == null) return;
            FunctionCollection functionCollection = module.Functions;

            rptChildren.DataSource = from function in functionCollection where function.Visible == true select function;
            rptChildren.DataBind();
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
          
            var context = ServiceContainer.GetService<ApplicationContext>();

            var service = ServiceContainer.GetService<ILogService>();
            service.Log(new LoggingEvent
            {
                ApplicationId = context.ApplicationId,
                Category = EventCategories.Security,
                Level = LogLevel.Logout,
                Source = "安全",
                UserName = context.UserName,
                Message =
                    "Logout account:" + context.UserName + Environment.NewLine +
                    "Logout Application:" +
                    SystemConfiguration.ApplicationName
            });
          
           Session.Abandon();   
            FormsAuthentication.SignOut();
            
            Response.Redirect(SiteUrls.Login);
        }


        protected void lbtnMailCount_Click(object sender, EventArgs e)
        {
           
         
        }

        public void radioBusinesType_CheckedChanged(object sender, EventArgs e)
        {

            var radioButton = (sender as RadioButton);
            if(radioButton.Checked)
            {
                if (radioButton.ID == "radioBusinesTypeAll")
                 {
                     (CurrentApplicationContext as OApplicationContext).BusinessType = BusinessType.All;
                     ;
                 }
                if (radioButton.ID == "radioBusinesTypeA")
                 {
                     (CurrentApplicationContext as OApplicationContext).BusinessType = BusinessType.A;
                     ;
                 }
                if (radioButton.ID == "radioBusinesTypeB")
                 {
                     (CurrentApplicationContext as OApplicationContext).BusinessType = BusinessType.B;
                     ;
                 }
            }
           
        }

    }
}
